//
//  FlowCube.m
//  丁诚昊的外包项目(2016_V7宝)
//
//  Created by DimChum_Haul on 16/3/25.
//  Copyright © 2016年 沙罗双树831. All rights reserved.
//

#import "FlowCube.h"
#import "Masonry.h"
#import "UIView+Geometry.h"
#import "UIView+PhyMoVal.h"
#import "UIColor+ColorFullTemplate.h"

#import "Pay4GDBlock.h"
#import "TTagButton.h"

#define CountPerLine 5
#define LineCount 8

@interface FlowCube () <UICollectionViewDelegateFlowLayout>
@property(assign) NSUInteger dataSourceNumber;
@end

@implementation FlowCube

static NSString * const reuseIdentifier = @"Cell";

- (instancetype)initWithBoNumber:(NSUInteger)boNumber
{
    if (self = [super init]) {
        UICollectionViewFlowLayout* flowLayout = [[UICollectionViewFlowLayout alloc]init];
        flowLayout.minimumLineSpacing = 5;
        flowLayout.minimumInteritemSpacing = 1;
        self = [[FlowCube alloc]initWithCollectionViewLayout:flowLayout];
        self.collectionView.contentInset = UIEdgeInsetsMake(0, 5, 0, 5);
        self.collectionView.backgroundColor = [UIColor clearColor];
        [self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:reuseIdentifier];
        
        //全局变量 记录数据源个数
        self.dataSourceNumber = boNumber > CountPerLine*LineCount? boNumber:CountPerLine*LineCount;
        return self;
    }
    return nil;
}

- (CGFloat) finalHeightOfFlow{
    UICollectionViewFlowLayout *Flow = (UICollectionViewFlowLayout*)self.collectionView.collectionViewLayout;
    CGFloat perW = (kscreenW - Flow.minimumInteritemSpacing - self.collectionView.contentInset.left - self.collectionView.contentInset.right) /CountPerLine;
    
    CGFloat finalHeightOfCoView = (perW + Flow.minimumLineSpacing) * LineCount;
    return finalHeightOfCoView;
}

#pragma mark <UICollectionViewDataSource>
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.dataSourceNumber;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier
                                                                           forIndexPath:indexPath];

    // Configure the cell
    cell.backgroundColor = [UIColor Heavy_ColorfulDay];
    [UIView simplifyWithCornerRadius:cell with:10.0f];
    UIView* view = [[TTagButton alloc] initWithIcon:@"lock"
                                          title:@"测试按钮"
                                    ActionBlock:nil];
    [cell addSubview:view];
    [view mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(cell.contentView);
    }];
    return cell;
}

#pragma mark <UICollectionViewDelegateFlowLayout>
- (CGSize) collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    UICollectionViewFlowLayout *Flow = (UICollectionViewFlowLayout*)collectionViewLayout;
    CGFloat perW = (kscreenW - Flow.minimumInteritemSpacing - collectionView.contentInset.left - collectionView.contentInset.right) / CountPerLine;
    return CGSizeMake( perW , perW );
}
@end
